import Cookies from "js-cookie";

// 设置setCookies；
export const setCookies = (key, val, cookieExpires) => {
    Cookies.set(key, val, { expires: cookieExpires || 1 });
};
// 获取getCookies；
export const getCookies = (key) => {
    return Cookies.get(key);
};

export const removeCookies = (key) => {
    return Cookies.remove(key);
};

/**
 * @param {String} url
 * @description 从URL中解析参数
 */
export const getParams = (url) => {
    const keyValueArr = url.split("?")[1].split("&");
    let paramObj = {};
    keyValueArr.forEach((item) => {
        const keyValue = item.split("=");
        paramObj[keyValue[0]] = keyValue[1];
    });
    return paramObj;
};

export const localSave = (key, value, json = false) => {
    if (json) {
        localStorage.setItem(key, JSON.stringify(value));
    } else {
        localStorage.setItem(key, value);
    }
};

export const localRead = (key, json = false) => {
    let value = localStorage.getItem(key) || "";
    if (json && value) {
        value = JSON.parse(value);
    }
    return value || "";
};

export const loadRemove = (key) => {
    localStorage.removeItem(key);
};
